/**
 * @author Renato Sales Bizerra
 * Created on 17/11/2005
 * Updated on 02/04/2010
 */
package br.unb.graphics.qos.charts;


import br.unb.core.traffic.Traffic;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import br.unb.graphics.qos.QosChart;

public abstract class GenericQosChart extends QosChart {

    /** Vectors with the last values*/
    protected HashMap vectorsMap;

    /**
     * Builds a controller QoS obj
     * @param title @see br.unb.chart.QosChart with known values for QoS parameters
     */
    public GenericQosChart(String title) {
        super(title);
        vectorsMap = new HashMap();
    }

    /**
     * Adds a traffic to the graphic
     * @param traffic name of the traffic to be added, must be unique
     */
    public synchronized void addTrafficToQos(String trafficName) {
        if (vectorsMap.get(trafficName) == null) {
            vectorsMap.put(trafficName, new Vector());
            Vector values = new Vector();
            values.add(new Value(0, 0));
            this.vectorsMap.put(trafficName, values);
            this.addTraffic(trafficName);
        }
    }

    /**
     * Adds a traffic to the graphic
     * @param traffic name of the traffic to be added, must be unique
     */
    public synchronized void removeTrafficFromQos(String trafficName) {
        this.vectorsMap.remove(trafficName);
        this.removeTraffic(trafficName);
    }

    /**
     * Adds a parameter to the QoS graphic
     */
    public abstract void addQosToTraffic(
            int nrPacket,
            int lenPacket,
            long sendTime,
            long receiveTime,
            String trafficName);

    /**
     * Adds values to the graphic from a file.
     * */
    public abstract void loadValuesFromFile(File file);

    /**
     * Adds Values to the vectors to the graphics
     */
    public void addValuesToCharts() {

        String name = "";
        Vector values;
        Value value;

        Iterator iterator = vectorsMap.keySet().iterator();

        while (iterator.hasNext()) {
            name = (String) iterator.next();
            values = (Vector) vectorsMap.get(name);

            if (values.size() > 0) {
                for (int i = 0; i < values.size(); i++) {
                    value = (Value) values.get(i);
                    this.addValueToTraffic(value.getValue(), value.getTimestamp(), name);
                }
            }
            vectorsMap.put(name, new Vector());
        }
    }

    class Value {

        private long timestamp;
        private double value;

        /**
         * @param timestamp
         * @param value
         */
        public Value(long timestamp, double value) {
            super();
            this.timestamp = timestamp;
            this.value = value;
        }

        public long getTimestamp() {
            return timestamp;
        }

        public double getValue() {
            return value;
        }
    }
}
